﻿using System;
using Microsoft.Office.Interop.Outlook;

namespace AnjLab.SyncIT.Core.Data.Outlook.Calendar
{
    public class OutlookCalendarItem : IOutlookItem
    {
        public string ItemID { get; set; }

        private AppointmentItem Item { get; set; }

        public OutlookCalendarItem(AppointmentItem item)
        {
            Item = item;
        }

        public bool Exists
        {
            get { return Item != null; }
        }

        public DateTime DueDate
        {
            get { return Exists ? Item.End : DateTime.MinValue; }
            set { if (Exists) Item.End = value; }
        }

        public DateTime StartDate
        {
            get { return Exists ? Item.Start : DateTime.MinValue; }
            set { if (Exists) Item.Start = value; }
        }

        public bool SupportsTimeRanging
        {
            get { return true; }
        }

        public string Categories
        {
            get { return Exists ? Item.Categories : string.Empty; }
            set { if (Exists) Item.Categories = value; }
        }

        public string Subject
        {
            get { return Exists ? Item.Subject : string.Empty; }
            set { if (Exists) Item.Subject = value; }
        }

        public string Body
        {
            get { return Exists ? Item.Body : string.Empty; }
            set { if (Exists) Item.Body = value; }
        }

        public bool Complete
        {
            //TODO refactor this
            get { return /*Exists ? Item.Complete : */false; }
            set { /*if (Exists) Item.Complete = value;*/ }
        }

        public int PercentComplete
        {
            get { return 0; }
            set { }
        }

        public OlImportance Importance
        {
            get { return Exists ? Item.Importance : OlImportance.olImportanceNormal; }
            set { if (Exists) Item.Importance = value; }
        }

        public DateTime LastModificationTime
        {
            get { return Exists ? Item.LastModificationTime : DateTime.MinValue; }
        }

        public void Save()
        {
            if (!deleted)
                Item.Save();
        }

        public void Close()
        {
            Library.Log.Debug("Deleteing Outlook calendar item: " + Item.Subject);

            Delete();
        }

        private bool deleted = false;
        public bool Deleted
        {
            get { return deleted; }
        }

        public void Delete()
        {
            Item.Delete();
            deleted = true;
        }
    }
}